<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="存货" prop="inventoryId">
        <el-select v-model="queryParams.inventoryId"  placeholder="请选择"  filterable    class="!w-240px">
              <el-option
                v-for="item in inventoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id!"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId"  placeholder="请选择"  filterable    class="!w-240px">
              <el-option
                v-for="item in warehouseList"
                :key="item.id"
                :label="item.name"
                :value="item.id!"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="货位" prop="locationId">
        <el-select v-model="queryParams.locationId"  placeholder="请选择"  filterable    class="!w-240px">
              <el-option
                v-for="item in locationList"
                :key="item.id"
                :label="item.name"
                :value="item.id!"
              />
            </el-select>
      </el-form-item>

      <el-form-item label="批号" prop="batchCode">
        <el-input
          v-model="queryParams.batchCode"
          placeholder="请输入批号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['wms:stock:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- 子表的列表 -->
      <!-- <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="stockLog">
            <el-tab-pane label="存货管理子" name="stockLog">
              <StockLogList :inventory-id="scope.row.inventoryId" :warehouse-id="scope.row.warehouseId" :location-id="scope.row.locationId" />
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column> -->
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="存货编号" align="center" prop="inventoryName" />
      <el-table-column label="存货名称" align="center" prop="inventoryCode" />
      <el-table-column label="存货规格" align="center" prop="inventorySpecModel" />
      <el-table-column label="仓库" align="center" prop="warehouseName" />
      <el-table-column label="货位" align="center" prop="locationName" />
      <el-table-column label="批次" align="center" prop="batchCode"  min-width="180px" />

      <el-table-column label="数量" align="center" prop="qty" />
      <!-- <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm(scope.row.id, scope.row.inventoryId,scope.row.locationId,scope.row.warehouseId)"
            v-hasPermi="['wms:stock:update']"
          >
            查看
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <StockForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { StockApi, StockVO } from '@/api/wms/stock'
import StockForm from './StockForm.vue'
import StockLogList from './components/StockLogList.vue'
import { WarehouseApi, WarehouseVO } from '@/api/wms/warehouse'
import { InventoryApi, InventoryVO } from '@/api/wms/inventory'
import { LocationApi, LocationVO } from '@/api/wms/location'

/** 存货管理 列表 */
defineOptions({ name: 'Stock' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<StockVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  inventoryId: undefined,
  warehouseId: undefined,
  locationId: undefined,
  batchCode:undefined,
  number: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const warehouseList = ref([] as WarehouseVO[]) // 
const inventoryList = ref([] as InventoryVO[]) // 
const locationList = ref([] as LocationVO[]) // 

//加载数据
onMounted(async () => {
  warehouseList.value= await WarehouseApi.getWarehouseSimpleList()
  inventoryList.value= await InventoryApi.getInventorySimpleList('')
  locationList.value= await LocationApi.getLocationSimpleList()


})
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StockApi.getStockPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (id: number, inventoryId: number,locationId:number,warehouseId:number) => {
  formRef.value.open(id, inventoryId,locationId,warehouseId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await StockApi.deleteStock(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await StockApi.exportStock(queryParams)
    download.excel(data, '存货管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
